﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_System_Mst_SysUserMaintenance
 * 类型：       系统人员维护维护
 * 功能说明：   设置系统人员的权限
 * 作者：       谈震鸣
 * 创建日期：   2007/08/14
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
 */
#region 引用
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System.Auth.Dao;
using Shpm.App.Commons.Db.Beans.Inp;
using Shpm.System.Auth.Beans;
using Shpm.System;
#endregion

public partial class App_System_Mst_SysUserMaintenance : System.Web.UI.Page
{
    bool systemFlg = false; //判断是该用户是否是超级用户或者是超级组的用户
   
    #region 常量
    private const string userClassNameAllow = "1";
    private const string userClassNameForbids = "2";
    # endregion

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    #endregion

    #region 检索按钮
    /*
     *名称： btnSelect_Click
     *功能： 左击检索按钮
     *参数： 无
     *返回： 无
     *说明： 无
    */
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        SysUserIdAllClear();
        if (ComValidator1.DoValidator()) SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 清除用户信息
    /*
     *名称： SysUserIdAllClear
     *功能： 画面初始化
     *参数： 无
     *返回： 无
     *说明： 无
    */
    private void SysUserIdAllClear()
    {
        txtSysUserId.Text = string.Empty;
        txtSysUserName.Text = string.Empty;
        txtPassWd.Text = string.Empty;
        txtConfirmPassWd.Text = string.Empty;
        lblSysUserId.Text = string.Empty;
        lblGroupSysUserId.Text = string.Empty;
        lstSetPermission.Items.Clear();
        lstSetGroup.Items.Clear();
        lstGetPermission.Items.Clear();
        lstGetGroup.Items.Clear();
    }
    #endregion
     
    #region 设置用户信息
    /*
     *名称： SelectSysUser
     *功能： 显示用户信息
     *参数： str 系统用户编号
     *返回： 无
     *说明： 无
    */
    private void SelectSysUser(string str)
    {
        if (str != string.Empty)
        {
            MstSysUserBean dataBean = new MstSysUserBean();
            dataBean.SysUserId = txtSysUserIdSel.Text;
            SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
            IList dataList = Dao.FindByMstSysUser(dataBean);
            if (dataList.Count > 0)
            {
                dataBean = (MstSysUserBean)dataList[0];
                txtSysUserId.Text = dataBean.SysUserId;
                txtSysUserName.Text = dataBean.SysUserId;
                lblSysUserId.Text = dataBean.SysUserId;
                lblGroupSysUserId.Text = dataBean.SysUserId;

                if (txtSysUserId.Text == Constants.SYSTEM_ID)
                {
                    systemFlg = true;
                }
                else
                {
                    InpUserGroupBean groupBean = new InpUserGroupBean();
                    groupBean.GroupId = Constants.SYSTEM_GROUP_ID;
                    groupBean.SysUserId = txtSysUserId.Text;
                    if (Dao.UserGroupLoad(groupBean) != null)
                    {
                        systemFlg = true;
                    }
                    else
                    {
                        systemFlg = false;
                    }
                }

                SetSelectAuthority(lblSysUserId.Text);
                SetUserAuthority(lblSysUserId.Text);
                SetSelectGroup(lblGroupSysUserId.Text);
                SetUserGroup(lblGroupSysUserId.Text);
            }
            else
            {
                string strErr = HttpContext.GetGlobalResourceObject("Message", "E0203") as string;
                ((Design_MasterPage)this.Master).InfoMessage = strErr.Replace("{0}", "用户");
            }
        }
    }
    #endregion

    #region 可赋予的权限
    /*
     *名称： SetSelectAuthority
     *功能： 将符合条件的数据显示在可赋予的权限的LIST中
     *参数： str 系统用户编号
     *返回： 无
     *说明： 无
    */
    private void SetSelectAuthority(string str)
    {
        if (str != string.Empty && systemFlg == false)
        {
            ClassNameBean dataBean = new ClassNameBean();
            SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
            dataBean.SysUserId = str;
            IList dataList = Dao.FindByClassName(dataBean);
            if (dataList.Count>0)
            {
                IEnumerator enm = dataList.GetEnumerator();
                while (enm.MoveNext())
                {
                    dataBean = enm.Current as ClassNameBean;
                    ListItem itm = new ListItem(dataBean.Display, dataBean.ClassName);
                    lstSetPermission.Items.Add(itm);
                    if (dataBean.UserClassName != null)
                    {
                        lstSetPermission.Items[lstSetPermission.Items.Count - 1].Attributes["style"] = "color:red";
                    }
                    else
                    {

                    }
                }
            }
        }
    }
    #endregion
    
    #region 已赋予的权限
    /*
     *名称： SetUserAuthority
     *功能： 将符合条件的数据显示在已赋予的权限的LIST中
     *参数： str 系统用户编号
     *返回： 无
     *说明： 无
    */
    private void SetUserAuthority(string str)
    {
        if (str != string.Empty && systemFlg == false)
        {
            ClassNameBean dataBean = new ClassNameBean();
            SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
            dataBean.SysUserId = str;
            IList dataList = Dao.FindByClassNameUser(dataBean);
            if (dataList.Count > 0)
            {
                IEnumerator enm = dataList.GetEnumerator();
                while (enm.MoveNext())
                {
                    dataBean = enm.Current as ClassNameBean;
                    ListItem itm = new ListItem(dataBean.Display, dataBean.ClassName);
                    lstGetPermission.Items.Add(itm);
                    if (dataBean.UserClassName != null)
                    {
                        lstGetPermission.Items[lstGetPermission.Items.Count - 1].Attributes["style"] = "color:red";
                    }
                }
            }
        }
        else if (str != string.Empty)
        {
            ClassNameBean dataBean = new ClassNameBean();
            SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
            dataBean.SysUserId = str;
            IList dataList = Dao.FindByAllClassName(dataBean);
            if (dataList.Count > 0)
            {
                IEnumerator enm = dataList.GetEnumerator();
                while (enm.MoveNext())
                {
                    dataBean = enm.Current as ClassNameBean;
                    ListItem itm = new ListItem(dataBean.Display, dataBean.ClassName);
                    lstGetPermission.Items.Add(itm);
                }
            }
        }
    }
    #endregion
    
    #region 可隶属的部门
    /*
     *名称： SetUserGroup
     *功能： 将符合条件的数据显示在可隶属的部门的LIST中
     *参数： str 系统用户编号
     *返回： 无
     *说明： 无
    */

    private void SetUserGroup(string str)
    {
        if (str != string.Empty)
        {
            InpUserGroupBean dataBean = new InpUserGroupBean();
            dataBean.SysUserId = str;
            SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
            IList dataList = Dao.FindByUserGroup(dataBean);
            if (dataList.Count > 0)
            {
                IEnumerator enm = dataList.GetEnumerator();
                while (enm.MoveNext())
                {
                    dataBean = enm.Current as InpUserGroupBean;
                    ListItem itm = new ListItem(dataBean.GroupName, dataBean.GroupId);
                    lstGetGroup.Items.Add(itm);
                }
            }
        }
    }
    #endregion
    
    #region 已隶属的部门
    /*
     *名称： SetSelectGroup
     *功能： 将符合条件的数据显示在已隶属的部门的LIST中
     *参数： str 系统用户编号
     *返回： 无
     *说明： 无
    */
    private void SetSelectGroup(string str)
    {
        if (str != string.Empty)
        {
            MstGroupBean dataBean = new MstGroupBean();
            dataBean.SysUserId = str;
            SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
            IList dataList = Dao.FindByMstGroup(dataBean);
            if (dataList.Count > 0)
            {
                IEnumerator enm = dataList.GetEnumerator();
                while (enm.MoveNext())
                {
                    dataBean = enm.Current as MstGroupBean;
                    ListItem itm = new ListItem(dataBean.GroupName, dataBean.GroupId);
                    lstSetGroup.Items.Add(itm);
                }
            }
        }
    }
    #endregion

    #region 添加权限按钮
    /*
     *名称： btnPermissionAdd_Click
     *功能： 左击添加权限按钮
     *参数： 无
     *返回： 无
     *说明： 添加用户权限
    */
    protected void btnPermissionAdd_Click(object sender, EventArgs e)
    {
        if (lstSetPermission.SelectedIndex != -1 && systemFlg==false )
        {
            lstGetPermission.SelectedIndex = -1;
            int i = 0;
            IEnumerator enm = lstSetPermission.Items.GetEnumerator();
            while (enm.MoveNext())
            {
                ListItem itm = enm.Current as ListItem;
                if (itm.Selected==true)
                {
                    IList lst =new ArrayList() ;
                    InpUserPermissionBean dataBean = new InpUserPermissionBean();
                    SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                    dataBean.SysUserId = lblSysUserId.Text;
                    dataBean.ClassName = itm.Value;
                    SysUserManager manager = new SysUserManager(Context);
                    SysUserBean sysUserBean = manager.getSysUserBean();
                    MstSysUserBean mstSysUserBean = sysUserBean.getMstSysUserBean();
                    dataBean.UserId = mstSysUserBean.SysUserId;
                    lst.Add(dataBean);
                    Dao.UpdateSysUserLst(lst, userClassNameAllow);
                    /*
                     if (Dao.Load(dataBean)!=null)
                    {
                        dataBean.Allow = userClassNameAllow;
                        Dao.UpdateUserPermission(dataBean);
                    }
                    else
                    {
                        dataBean.Allow = userClassNameAllow;
                        Dao.InsertUserPermission(dataBean);
                    }
                     */
                    lstGetPermission.Items.Add(itm);
                    i++;
                }
            }
            for (int j = 0; j <= i; j++)
            {
                lstSetPermission.Items.Remove(lstSetPermission.SelectedItem);
            }
            if (lstSetPermission.Items.Count > 0)
            {
                lstSetPermission.SelectedIndex = 0;
            }
            else
            {
                lstSetPermission.SelectedIndex = -1;
            }
        }
        SysUserIdAllClear();
        SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 删除权限按钮
    /*
     *名称： btnPermissionDel_Click
     *功能： 左击删除权限按钮
     *参数： 无
     *返回： 无
     *说明： 删除用户权限
    */
    protected void btnPermissionDel_Click(object sender, EventArgs e)
    {
        if (lstGetPermission.SelectedIndex != -1 && systemFlg==false)
        {
            lstSetPermission.SelectedIndex = -1;
            int i=0;
            IEnumerator enm = lstGetPermission.Items.GetEnumerator();
            while (enm.MoveNext())
            {
                ListItem itm = enm.Current as ListItem;
                if (itm.Selected == true)
                {
                    IList lst = new ArrayList();
                    InpUserPermissionBean dataBean = new InpUserPermissionBean();
                    SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                    dataBean.SysUserId = lblSysUserId.Text;
                    dataBean.ClassName = itm.Value;
                    SysUserManager manager = new SysUserManager(Context);
                    SysUserBean sysUserBean = manager.getSysUserBean();
                    MstSysUserBean mstSysUserBean = sysUserBean.getMstSysUserBean();
                    dataBean.UserId = mstSysUserBean.SysUserId;
                    lst.Add(dataBean);
                    Dao.UpdateSysUserLst(lst, userClassNameForbids);
                    /*
                    if (Dao.Load(dataBean) != null)
                    {
                        dataBean.SysUserId = lblSysUserId.Text;
                        dataBean.ClassName = itm.Value;
                        dataBean.Allow = userClassNameForbids;
                        Dao.UpdateUserPermission(dataBean);
                    }
                    else
                    {
                        dataBean.SysUserId = lblSysUserId.Text;
                        dataBean.ClassName = itm.Value;
                        dataBean.Allow = userClassNameForbids;
                        Dao.InsertUserPermission(dataBean);
                    }
                     */
                    lstSetPermission.Items.Add(itm);
                    i++;
                }
            }
            for (int j = 0; j <= i; j++)
            {
                lstGetPermission.Items.Remove(lstGetPermission.SelectedItem);
            }
            if (lstGetPermission.Items.Count > 0)
            {
                lstGetPermission.SelectedIndex = 0;
            }
            else
            {
                lstGetPermission.SelectedIndex = -1;
            }
        }
        SysUserIdAllClear();
        SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 添加部门按钮
    /*
     *名称： btnGroupAdd_Click
     *功能： 左击添加部门按钮
     *参数： 无
     *返回： 无
     *说明： 将用户添加到部门中
    */
    protected void btnGroupAdd_Click(object sender, EventArgs e)
    {
        if (lstSetGroup.SelectedIndex != -1)
        {
            int i=0;
            lstGetGroup.SelectedIndex = -1;
            IEnumerator enm = lstSetGroup.Items.GetEnumerator();
            while (enm.MoveNext())
            {
                ListItem itm = enm.Current as ListItem;
                if (itm.Selected == true)
                {
                    IList lst = new ArrayList();
                    InpUserGroupBean dataBean = new InpUserGroupBean();
                    dataBean.GroupId = itm.Value;
                    dataBean.SysUserId = lblGroupSysUserId.Text;
                    SysUserManager manager = new SysUserManager(Context);
                    SysUserBean sysUserBean = manager.getSysUserBean();
                    MstSysUserBean mstSysUserBean = sysUserBean.getMstSysUserBean();
                    dataBean.UserId = mstSysUserBean.SysUserId;
                    lst.Add(dataBean);
                    SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                    Dao.InsertUserGroupLst(lst);
                    lstGetGroup.Items.Add(itm);
                    i++;
                }
            }
            for (int j = 0; j <= i; j++)
            {
                lstSetGroup.Items.Remove(lstSetGroup.SelectedItem);
            }
            if (lstSetGroup.Items.Count > 0)
            {
                lstSetGroup.SelectedIndex = 0;
            }
            else
            {
                lstSetGroup.SelectedIndex = -1;
            }
        }
        SysUserIdAllClear();
        SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 删除部门按钮
    /*
     *名称： btnGroupDel_Click
     *功能： 左击删除部门按钮
     *参数： 无
     *返回： 无
     *说明： 将用户从部门中删除
    */
    protected void btnGroupDel_Click(object sender, EventArgs e)
    {
        if (lstGetGroup.SelectedIndex != -1)
        {
            int i=0;
            lstSetGroup.SelectedIndex = -1;
            IEnumerator enm = lstGetGroup.Items.GetEnumerator();
            while (enm.MoveNext())
            {
                ListItem itm = enm.Current as ListItem;
                if (itm.Selected == true)
                {
                    InpUserGroupBean dataBean = new InpUserGroupBean();
                    dataBean.GroupId = itm.Value;
                    dataBean.SysUserId = lblGroupSysUserId.Text;
                    SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                    Dao.DeleteUserGroup(dataBean);
                    lstSetGroup.Items.Add(itm);
                    i++;
                }
            }
            for (int j = 0; j <= i; j++)
            {
                lstGetGroup.Items.Remove(lstGetGroup.SelectedItem);
            }
            if (lstGetGroup.Items.Count > 0)
            {
                lstGetGroup.SelectedIndex = 0;
            }
            else
            {
                lstGetGroup.SelectedIndex = -1;
            }
        }
        SysUserIdAllClear();
        SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 取消输入按钮
    /*
     *名称： btnCancel_Click
     *功能： 左击取消输入按钮
     *参数： 无
     *返回： 无
     *说明： 无
    */

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtPassWd.Text = string.Empty;
        txtConfirmPassWd.Text = string.Empty;
        SysUserIdAllClear();
        SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 重置权限按钮
    /*
     *名称： btnPermissionCancel_Click
     *功能： 左击重置权限按钮
     *参数： 无
     *返回： 无
     *说明： 无
    */
    protected void btnPermissionCancel_Click(object sender, EventArgs e)
    {
        if ((lstSetPermission.SelectedIndex != -1 || lstGetPermission.SelectedIndex != -1)  && systemFlg==false)
        {
            IList lst = lstSetPermission.Items;
            IEnumerator enmSet = lstSetPermission.Items.GetEnumerator();
            IEnumerator enmGet = lstGetPermission.Items.GetEnumerator();
            
            while (enmSet.MoveNext())
            {
                ListItem itm = enmSet.Current as ListItem;
                if (itm.Selected == true)
                {
                    InpUserPermissionBean dataBean = new InpUserPermissionBean();
                    SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                    dataBean.SysUserId = lblSysUserId.Text;
                    dataBean.ClassName = itm.Value;
                    Dao.DeleteUserPermission(dataBean);
                }
            }

            while (enmGet.MoveNext())
            {
                ListItem itm = enmGet.Current as ListItem;
                if (itm.Selected == true)
                {
                    InpUserPermissionBean dataBean = new InpUserPermissionBean();
                    SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                    dataBean.SysUserId = lblSysUserId.Text;
                    dataBean.ClassName = itm.Value;
                    Dao.DeleteUserPermission(dataBean);
                }
            }
        }
        SysUserIdAllClear();
        SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 登录按钮
    /*
     *名称： btnAdd_Click
     *功能： 左击登录按钮
     *参数： 无
     *返回： 无
     *说明： 更改该用户的密码
    */

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ComValidator2.DoValidator() && txtSysUserId.Text != string.Empty)
        {
            if (txtPassWd.Text == txtConfirmPassWd.Text)
            {
                MstSysUserBean dataBean = new MstSysUserBean();
                SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                dataBean.SysUserId = txtSysUserId.Text;
                dataBean.PassWd = txtPassWd.Text;
                SysUserManager manager = new SysUserManager(Context);
                SysUserBean sysUserBean = manager.getSysUserBean();
                MstSysUserBean mstSysUserBean = sysUserBean.getMstSysUserBean();
                dataBean.UserId = mstSysUserBean.SysUserId;
                Dao.UpdateSysUser(dataBean);
            }
            else
            {
                string str = HttpContext.GetGlobalResourceObject("Message", "E0004") as string;
                ((Design_MasterPage)this.Master).InfoMessage = str.Replace("{0}", "密码");

            }
        }
        SysUserIdAllClear();
        SelectSysUser(txtSysUserIdSel.Text);
    }
    #endregion

    #region 删除按钮
    /*
     *名称： btnDel_Click
     *功能： 左击删除按钮
     *参数： 无
     *返回： 无
     *说明： 删除该用户
    */
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (txtSysUserId.Text != string.Empty)
        {
                InpUserGroupBean dataBean1 = new InpUserGroupBean();
                InpUserPermissionBean dataBean2 = new InpUserPermissionBean();
                MstSysUserBean dataBean3 = new MstSysUserBean();
                SysUserMaintenanceDao Dao = new SysUserMaintenanceDao();
                dataBean1.SysUserId = txtSysUserId.Text;
                dataBean2.SysUserId = txtSysUserId.Text;
                dataBean3.SysUserId = txtSysUserId.Text;
                Dao.DeleteUserGroupUserId(dataBean1);
                Dao.DeleteUserPermissionUserId(dataBean2);
                Dao.DeleteSysUser(dataBean3);
                SysUserIdAllClear();
        }
    }
    #endregion
}
